package edu.uoc.tdp.pac4.client.pantalles;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EtchedBorder;

import edu.uoc.tdp.pac4.common.beans.S1Usuari;
import edu.uoc.tdp.pac4.common.beans.S2ConsultaStock;
import edu.uoc.tdp.pac4.common.exception.ExcepcioConnexioBD;
import edu.uoc.tdp.pac4.common.interfaces.S2Interface;
import edu.uoc.tdp.pac4.common.utils.TDSLanguageUtils;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.rmi.RemoteException;

public class S2RecepComanda extends JPanel {
	private JTextField textFieldCodiPeca;
	private JTextField textFieldDescrip;
	private JTextField textFieldPVP;
	private JTextField textFieldPCD;
	private JTextField textFieldUnitatsRebudes;
	
	
	private final JFrame parent;
	private final S2Interface s2Interface;
	private final S1Usuari usuariConnectat;

	/**
	 * Create the panel.
	 */
	public S2RecepComanda(JFrame parent, S2Interface s2Interfac, S1Usuari usuariConnect) {
		
		
		this.parent = parent;
		this.s2Interface=s2Interfac;
		this.usuariConnectat = usuariConnect;
		
		JPanel panel = new JPanel();
		panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 430, GroupLayout.PREFERRED_SIZE)
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 429, GroupLayout.PREFERRED_SIZE)
						.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 433, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 99, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(27, Short.MAX_VALUE))
		);
		
		JLabel lblUnitatsRebudes = new JLabel(TDSLanguageUtils.getMessage("LBL_UNIT_REB"));
		
		textFieldUnitatsRebudes = new JTextField();
		textFieldUnitatsRebudes.setColumns(10);
		
		JButton btnActualitzaStock = new JButton(TDSLanguageUtils.getMessage("BTN_ACT_STOCK"));
		btnActualitzaStock.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				consultaPeca ();
				
				S2ConsultaStock c = consultaPeca();
				
				if (c != null){
					
					try {
						S2RecepComanda.this.s2Interface.incrementaStock(S2RecepComanda.this.usuariConnectat.getTaller(),Integer.parseInt(textFieldCodiPeca.getText()) ,Integer.parseInt(textFieldUnitatsRebudes.getText()));
						
						JOptionPane.showMessageDialog(S2RecepComanda.this, 
								TDSLanguageUtils.getMessage("MSG_STOCK_ACTUALITZAT"), "SmartRepair", JOptionPane.INFORMATION_MESSAGE);
						
						//Buidem el contingut del panel
						S2RecepComanda panelNou = new S2RecepComanda(S2RecepComanda.this.parent, S2RecepComanda.this.s2Interface, S2RecepComanda.this.usuariConnectat);
						S2RecepComanda.this.parent.setContentPane(panelNou);
						S2RecepComanda.this.parent.pack();
					
					} catch (NumberFormatException e) {
						JOptionPane.showMessageDialog(S2RecepComanda.this, 
								TDSLanguageUtils.getMessage("MSG_ERROR_FORMAT"), "SmartRepair", JOptionPane.ERROR_MESSAGE);	
						e.printStackTrace();
					} catch (RemoteException e) {
						JOptionPane.showMessageDialog(S2RecepComanda.this, 
								TDSLanguageUtils.getMessage("MSG_ERROR_CONS_PECA"), "SmartRepair", JOptionPane.ERROR_MESSAGE);	
						e.printStackTrace();
					}

				}
				
			}
		});
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblUnitatsRebudes)
					.addGap(18)
					.addComponent(textFieldUnitatsRebudes, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(59)
					.addComponent(btnActualitzaStock)
					.addContainerGap(95, Short.MAX_VALUE))
		);
		gl_panel_2.setVerticalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addGap(31)
					.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblUnitatsRebudes)
						.addComponent(textFieldUnitatsRebudes, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnActualitzaStock))
					.addContainerGap(54, Short.MAX_VALUE))
		);
		panel_2.setLayout(gl_panel_2);
		
		JLabel lblDescricio = new JLabel(TDSLanguageUtils.getMessage("LBL_DESCR"));
		
		textFieldDescrip = new JTextField();
		textFieldDescrip.setEditable(false);
		textFieldDescrip.setColumns(10);
		
		JLabel lblPvp = new JLabel(TDSLanguageUtils.getMessage("LBL_PVP"));
		
		textFieldPVP = new JTextField();
		textFieldPVP.setEditable(false);
		textFieldPVP.setColumns(10);
		
		JLabel lblNewLabel = new JLabel(TDSLanguageUtils.getMessage("LBL_PVD"));
		
		JLabel lblComproveuDades = new JLabel(TDSLanguageUtils.getMessage("LBL_COMPR_DADES"));
		
		textFieldPCD = new JTextField();
		textFieldPCD.setEditable(false);
		textFieldPCD.setColumns(10);
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addComponent(textFieldDescrip, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblDescricio))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addComponent(lblPvp)
								.addComponent(textFieldPVP, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(6)
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel)
								.addComponent(textFieldPCD, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addComponent(lblComproveuDades))
					.addGap(168))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(6)
					.addComponent(lblComproveuDades)
					.addGap(18)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblDescricio)
						.addComponent(lblPvp)
						.addComponent(lblNewLabel))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(textFieldDescrip, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(textFieldPVP, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(textFieldPCD, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		panel_1.setLayout(gl_panel_1);
		
		JLabel lblIntroduiuElCodi = new JLabel(TDSLanguageUtils.getMessage("LBL_INTR_COD_PECA"));
		
		textFieldCodiPeca = new JTextField();
		textFieldCodiPeca.setColumns(10);
		
		JButton btnCerca = new JButton(TDSLanguageUtils.getMessage("BTN_CONSULTA"));
		btnCerca.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				consultaPeca ();
				
				
					
				
				
				
				
			}
		});
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblIntroduiuElCodi)
					.addGap(30)
					.addComponent(textFieldCodiPeca, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(38)
					.addComponent(btnCerca)
					.addContainerGap(47, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblIntroduiuElCodi)
						.addComponent(textFieldCodiPeca, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnCerca))
					.addContainerGap(30, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		setLayout(groupLayout);

	}
	
	public S2ConsultaStock consultaPeca (){
		
		S2ConsultaStock c = null;
		
		try {
			
			c = s2Interface.consultaStockS2(usuariConnectat.getTaller(), Integer.parseInt(textFieldCodiPeca.getText()));
			
			textFieldDescrip.setText(c.getDescripcio());
			textFieldPVP.setText(""+c.getPvp());
			textFieldPCD.setText(""+c.getPvd());
			
			
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(S2RecepComanda.this, 
					TDSLanguageUtils.getMessage("MSG_ERROR_FORMAT"), "SmartRepair", JOptionPane.ERROR_MESSAGE);	
			e.printStackTrace();
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(S2RecepComanda.this, 
					TDSLanguageUtils.getMessage("MSG_ERROR_CONS_PECA"), "SmartRepair", JOptionPane.ERROR_MESSAGE);	
			e.printStackTrace();
		} catch (ExcepcioConnexioBD e) {
			JOptionPane.showMessageDialog(S2RecepComanda.this, 
					TDSLanguageUtils.getMessage("MSG_ERROR_CONS_PECA"), "SmartRepair", JOptionPane.ERROR_MESSAGE);	
			e.printStackTrace();
		}
		
		return c;
	}
}
